import type { CreateComponentType } from "@/views/dataview/packages/public/type"
import { useDataViewStore } from "@/stores/dataview"
import { computed, watch, type ComputedRef } from "vue"
import type { RequestDataPondItemType, RequestGlobalConfigType } from "@/stores/types"
import type { RequestParamsTypeEnum } from "@/const"
import { intervalUnitHandle } from "@/utils"

type ChartEditStoreType = typeof useDataViewStore

// 数据池存储的数据类型
type DataPondMapType = {
    updateCallback: (...args: any) => any
    filter?: string | undefined
}

// 数据池map对象，请求对应callback
const mittDataPondMap = new Map<string,DataPondMapType[]>()

// 创建单个数据项轮询接口
const newPondItemInterval = (
    requestGlobalConfig: RequestGlobalConfigType,
    requestDataPondItem: ComputedRef<RequestDataPondItemType>,
    dataPondMapItem?: DataPondMapType[]
)=>{
    if(!dataPondMapItem) return
    let fetchInterval = 0
    clearInterval(fetchInterval)

    const fetchFn = async ()=>{
        console.log("自定义请求触发");
    }

    // 监听请求参数变化重新请求
    watch(
        ()=>requestDataPondItem.value.dataPondRequestConfig.requestParams.Params,
        ()=>{
            fetchFn()
        },
        {
            immediate:true,
            deep:true
        }
    )

    // 立即调用
    fetchFn()

    // 自己的
    const targetInterval = requestDataPondItem.value.dataPondRequestConfig.requestInterval
    const targetUnit = requestDataPondItem.value.dataPondRequestConfig.requestIntervalUnit

    // 全局的
    const globalRequestInterval = requestGlobalConfig.requestInterval
    const globalUnit = requestGlobalConfig.requestIntervalUnit

    const time = targetInterval?targetInterval : globalRequestInterval
    const unit = targetUnit?targetUnit : globalUnit

    if(time){
        // 开启轮询 intervalUnitHandle格式化单位时间
        fetchInterval = setInterval(fetchFn, intervalUnitHandle(time,unit));
    }
}

export const useChartDataPondFetch = ()=>{
    // 新增全局接口
    const addGlobalDataInterface = (
        targetComponent:CreateComponentType,
        useDataViewStore:ChartEditStoreType,
        updateCallback: (...args: any) => any
    )=>{
        const dataviewStore = useDataViewStore()
        const requestDataPondId = targetComponent.request.requestDataPondId as string
        // 组件对应数据池id
        const mittPondIdArr = mittDataPondMap.get(requestDataPondId) || []
        mittPondIdArr.push({
            updateCallback:updateCallback,
            filter:targetComponent.filter
        })
        mittDataPondMap.set(requestDataPondId,mittPondIdArr)
    }

    // 清除旧数据
    const clearMittDataPondMap = ()=>{
        mittDataPondMap.clear()
    }

    // 初始化数据池
    const initDataPond = (useDataViewStore:ChartEditStoreType)=>{
        const dataviewStore = useDataViewStore()
        const requestGlobalConfig = dataviewStore.dataviewState.requestGlobalConfig
        // 根据id查找数据池
        for (const pondKey of mittDataPondMap.keys()) {
            const requestDataPondItem = computed(()=>{
                return requestGlobalConfig.requestDataPond.find(item=>item.dataPondId === pondKey)
            }) as ComputedRef<RequestDataPondItemType>
            if(requestDataPondItem.value){
                newPondItemInterval(requestGlobalConfig,requestDataPondItem,mittDataPondMap.get(pondKey))
            }
        }
    }
    return {
        addGlobalDataInterface,
        clearMittDataPondMap,
        initDataPond
    }
}